﻿package {
	
	import flash.text.*;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	
	class NumberSelectBox extends Sprite {
		
		private var maxSize:Number;
		private var minSize:Number;
		private var scrollSpeed:Number;
		
		public var val:Number;
		public var input:TextField;
		
		public function NumberSelectBox(X:int, Y:int, W:int, H:int, max:Number = 20, min:Number = 0, speed:Number = 2, defaultVal:Number = 10) {
			maxSize = max;
			minSize = min;
			scrollSpeed = speed;
			
			input = new TextField();
			input.x = X; input.y = Y; input.width = W; input.height = H;
			input.text = String(defaultVal);
			input.addEventListener(MouseEvent.MOUSE_WHEEL, scrollListener);			
			input.border = true; input.backgroundColor = 0xFFFFFF; input.background = true; input.type = TextFieldType.INPUT;
			val = defaultVal;
		}
		
		private function scrollListener(e:MouseEvent) {
			var disp = scrollSpeed * e.delta / 3;
			val += disp;
			if (val > maxSize) val = maxSize;
			if (val < minSize) val = minSize;
			input.text = String(val);
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function setSize(val:Number) {
			this.val = val;
			input.text = String(val);
		}
	}
}